package com.project.erp.dao.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Param;

@Repository
public class ParamDao extends HibernateDao<Param, Long> {
	/**
	 * 根据id列表删除
	 * @param idsList
	 */
	public void delParamByparamIds(final List<Long> idsList){
		this.deleteByIdsList(idsList);
	}
	
	/**
	 * 根据系统参数标识查找系统参数记录
	 * @param nameEn
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Param getParamByItemEn(String nameEn){
		Criteria c = this.getSession().createCriteria(Param.class);
		c.add(Restrictions.eq("nameEn", nameEn));

		List<Param> params = c.list();
		Param param = null;
		
		if((params != null) && (params.size() > 0)){
			param = params.get(0);
		}
		return param;
	}
	
	
	/**
	 * 根据条件查询
	 * @param page
	 * @param param
	 * @return
	 */
	public Page<Param> getParamPageBySearch(Page<Param> page,Param param){
		if(null == param){
			page = this.getAll(page);
		}
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(param.getNameEn())){
			Criterion nameEn = Restrictions.ilike("nameEn",param.getNameEn(),MatchMode.ANYWHERE);
			list.add(nameEn);
		}
		if(StringUtils.isNotEmpty(param.getNameZn())){
			Criterion nameZn = Restrictions.ilike("nameZn",param.getNameZn(),MatchMode.ANYWHERE);
			list.add(nameZn);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
}
